/**
 * AsyncSeriesWaterfallHook 异步串行瀑布钩子，像瀑布一样的传递参数
 */
const { AsyncSeriesWaterfallHook } = require('tapable');
// const { AsyncSeriesWaterfallHook } = require('../real-tapable');

class Hook {
    constructor() {
        this.hooks = {
            arch: new AsyncSeriesWaterfallHook(['arg1'])
        }
    }
    // 注册监听函数
    subscribe() {
        this.hooks.arch.tapAsync('primary', (name, cb) => {
            setTimeout(() => {
                console.log('初级前端：', name);
                cb(null, 'cheney')
            }, 1000)
        })
        this.hooks.arch.tapAsync('intermediate', (data, cb) => {
            setTimeout(() => {
                console.log('中级前端：', data);
                cb('error', 'xiaopa')
            }, 1000)
        })
        this.hooks.arch.tapAsync('senior', (data, cb) => {
            setTimeout(() => {
                console.log('高级前端：', data);
                cb(null)
            }, 1000)
        })
    }
    // 执行监听函数
    publish() {
        this.hooks.arch.callAsync('yxfan', () => {
            console.log('封神啦!');
        })
    }
}
// const hook = new Hook();
// hook.subscribe();
// hook.publish();

class HookPromise {
    constructor() {
        this.hooks = {
            arch: new AsyncSeriesWaterfallHook(['arg1'])
        }
    }
    // 注册监听函数
    subscribe() {
        this.hooks.arch.tapPromise('primary', (name) => {
            return new Promise((resolve) => {
                setTimeout(() => {
                    console.log('初级前端：', name);
                    resolve('cheney')
                }, 1000)
            })
        })
        this.hooks.arch.tapPromise('intermediate', (data) => {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log('中级前端：', data);
                    // resolve('xiaopa')
                    reject()
                }, 1000)
            })
        })
        this.hooks.arch.tapPromise('senior', (data) => {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log('高级前端：', data);
                    resolve()
                }, 1000)
            })
        })
    }
    // 执行监听函数
    publish() {
        this.hooks.arch.promise('yxfan')
            .then(() => {
                console.log('封神啦！');
            })
    }
}
const hookPromise = new HookPromise();
hookPromise.subscribe();
hookPromise.publish();